package cn1shared.ui;

import com.codename1.ui.Button;
import com.codename1.ui.Font;
import com.codename1.ui.Graphics;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.table.TableLayout;

/**
 * Label with two rows
 */
public abstract class DoubleLabel extends Button {

	private static final int SPACING = 5;
	private int subTitleColor = 0x888888;

	public DoubleLabel() {
		TableLayout layout = new TableLayout(2, 1);
		layout.setGrowHorizontally(true);
		FormUtils.setHeight(this, 3f);
		getStyle().setBgTransparency(0x00);
		addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				clicked();
			}
		});
	}

	protected abstract void clicked();

	private void red() { subTitleColor = 0x880000; }
	private void gray() { subTitleColor = 0x888888; }

	protected String check(String value, String ifNull) {
		if (value == null) {
			red();
			return ifNull;
		}
		gray();
		return value;
	}

	public abstract String getTitle();
	public abstract String getSubtitle();

	public void paint(Graphics g) {
		super.paintBackground(g);
		String t = getTitle();
		Font lFont = FormUtils.getFont("large");
		Font mFont = FormUtils.getFont("medium");

		int totalh = lFont.getHeight() + mFont.getHeight() + SPACING;
		int middleY = getY() + getHeight() / 2;
		int posY = middleY - totalh / 2;

		int posX = getX() + 5;

		g.setFont(lFont);
		g.setColor(0x000000);
		g.drawString(t, posX, posY);

		String s = getSubtitle();
		s = s == null? "" : s;
		g.setFont(mFont);
		g.setColor(subTitleColor);
		g.drawString(s, posX, posY + SPACING + lFont.getHeight());
	}
}